﻿<?php


class UsertypeApp extends BaseApp
{

    var $type = 'usertype';
    var $table_id = 0;
	var $view_data = array();
	var $view_scup_dir = '';

	function __construct() 
	{
      $this->UsertypeApp();
	}
		
	function UsertypeApp() 
	{
	   $this->view_scup_dir = ROOT_PATH.'/scup/view/'.VIEW_TYPE;   
	   parent::__construct();
	}	
	

    function index()
    {

        $roledata = $this->get_role_data();

		$TB_module = &$this->_model('usertype'); 		
		$infos = $TB_module->show();

        $this->assign('infos', $infos);			
        $this->assign('roledata', $roledata);		
    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('usertype_list.view.php');
    }
	

	
    function edit()
    {	

		$id = $_GET['id'];
		$TB_module = &$this->_model('usertype'); 	 	 
		
		$data = $TB_module->oneshow($id);


	    $this->assign('data', $data);		

        $roledata = $this->get_role_data();
        $this->assign('roledata', $roledata);		
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('usertype_edit.view.php');
    }

    function addpost()
    {
	     $data = $_POST['data'];

		 $TB_module = &$this->_model('usertype'); 
	 	 
         $maxid = $TB_module->get_module_max_id();
		 unset($data['id']);
         $data['id'] = $maxid+1;
		 if(!isset($data['config']))
		 {
		 $data['config'] ='';
		 }
		 $data['config'] = serialize($data['config']);

		 $TB_module->post($data);
         
		 $db = & get_db();
		 if($db->table_exists(DB_PREFIX."usertype_".$data['id'].""))
		 {
		 $notice = &get_message(array('msg'=>'此数据库表已经存在了！','url'=>'','title'=>'Notice'));
		 $notice ->display();			 
		 }



         $SQL ='';	
	     if(DB_CHARSET && $db->version() > '4.1' )
		 {	
		  $SQL=" DEFAULT CHARSET=".DB_CHARSET." ";	
	     }
	
	     $sql="CREATE TABLE `".DB_PREFIX."usertype_".$data['id']."` (
        `id` mediumint(7) unsigned NOT NULL auto_increment COMMENT '主表id',
        `user_id` int(10) NOT NULL default '0' COMMENT '发布者id',
        `user_name` varchar(50) NOT NULL default '0' COMMENT '发布者用户名',
        `add_time` int(10) NOT NULL default '0' COMMENT '添加时间',		
         PRIMARY KEY  (`id`)
     	) ENGINE=MyISAM ".$SQL." AUTO_INCREMENT=1 ;";	
		
		
    	$db->query($sql);
					 
         $this->write_cache();
		 //$this->write_power_cache();
         $this->write_module_default_html($data['id']);		 
		 $this->write_module_default_scup_html($data['id']);
		 $this->index();

    }

    function del()
    {

	     $id = $_GET['id'];
		 $db = & get_db();

	     $rs=$db->get_one("SELECT count(*) AS num FROM ".DB_PREFIX."usertype_{$id}  WHERE user_id>0 and user_name!='0' ");	
	     if($rs['num'])
		 {	
		 $notice = &get_message(array('msg'=>'本模块已有内容了,请先删除内容','url'=>'','title'=>'Notice'));
		 $notice ->display();			
	     }		 

		 $TB_module = &$this->_model('usertype'); 		 
		 $TB_module->del($id); 

		 if(!$db->table_exists(DB_PREFIX."usertype_{$id}"))
		 {
		 $notice = &get_message(array('msg'=>'此数据库表已经不存在了！','url'=>'','title'=>'Notice'));
		 $notice ->display();			 
		 }
	     $db->query(" DROP TABLE IF EXISTS `".DB_PREFIX."usertype_{$id}`");		 
		 
         $this->write_cache();
		// $this->write_power_cache();


         $dir_array = array($this->view_scup_dir.'/usertype/usertype_list.view.html',
		 $this->view_scup_dir.'/usertype/usertype_add.view.html',
		 $this->view_scup_dir.'/usertype/usertype_edit.view.html',
		 $GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_list.view.html",
		 $GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_add.view.html",
		 $GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_edit.view.html",
		 );
		 foreach ($dir_array as $key => $value)
		 {
		    if(!is_dir($value)&&file_exists($value))
		    {
		     del_file($value);	
		    }		 
		 }	
				 	
		 $this->index();

    }
	


    function editpost()
    {

	     $data = $_POST['data'];
		 
		 $id = intval($data['id']);	
		 $TB_module = &$this->_model('usertype'); 
		 $TB_module->editaction($id,$data);	 
         $this->write_cache();	
		 //$this->write_power_cache();
	     $this->produced_html($id);		  
		 $this->index();
		 

    }


   function filedlist($id=0)
    {
	    if($id==0)
		{
        $id = intval($_GET['id']);
		}
		
		$TB_module = &$this->_model('usertype'); 	 
		
		$data = $TB_module->oneshow($id);
		
		$field_config = unserialize($data['config']);
		
        $this->assign('fieldconfig', $field_config);		
 
        $this->assign('id', $id);	   
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('usertype_filedlist.view.php');
    }
	
	
    function filedadd()
    {	
        $id = intval($_GET['id']);	
        $sxarray1 = get_all_dirname(ROOT_PATH . '/data/sx');
		$sxarray = array();
	    foreach($sxarray1  as $key => $value)
		{
		   if(strpos($value,'.php') && strstr($value,'sx_'))
		   {
		      $strid = str_replace(array('.php','sx_'),array('',''),$value);
			  $tempname = $this->get_sx_cache($strid); 
		      $sxarray[$tempname] = $value;
		   }
		}		

        $this->assign('sxarray', $sxarray);
			
		$mdivdata = $this->get_module_data();

        $this->assign('mdivdata', $mdivdata);
	
        $this->assign('id', $id);				
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('usertype_filedadd.view.php');
    }

    function filededit()
    {	
        $id = intval($_GET['id']);	
        $tbid = intval($_GET['tbid']);

        $sxarray1 = get_all_dirname(ROOT_PATH . '/data/sx');
		$sxarray = array();
	    foreach($sxarray1  as $key => $value)
		{
		   if(strpos($value,'.php') && strstr($value,'sx_'))
		   {
		      $strid = str_replace(array('.php','sx_'),array('',''),$value);
			  $tempname = $this->get_sx_cache($strid); 
		      $sxarray[$tempname] = $value;
		   }
		}		

        $this->assign('sxarray', $sxarray);
		 $TB_module = &$this->_model('usertype'); 	 
         $field_one_info = $TB_module->oneshow($id);
	     $field_config_data = unserialize($field_one_info['config']);
	     $data = $field_config_data[$tbid];
		 if(empty($data)||!is_array($data))
		 {
           $data = array();
		 }

        $this->assign('data', $data);			
			
		$mdivdata = $this->get_module_data();
        $this->assign('mdivdata', $mdivdata);
	
        $this->assign('id', $id);
        $this->assign('tbid', $tbid);						
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
        $this->display('usertype_filededit.view.php');
    }



    function filedpostadd()
    {

         $id = intval($_POST['id']);

	      $data= $_POST['data'];
		 

		 $tempdata = $temptype = '';
		 $temptype = $data['form']['type'];
		 $tempdata = $data['form'][$data['form']['type']];
		 unset($data['form']);
		 $data['form']['type'] = $temptype;
		 $data['form']['icon'] = $data['filed']['cname'];			 
		 $data['form'][$data['form']['type']] = $tempdata;
		 $data['form'][$data['form']['type']]['name'] = 'data['.$data['filed']['name'].']';	
		 $data['form']['sx_data'] = $data['sx_data'];
		 unset($data['sx_data']);

	 
	    if(!preg_match("/^([a-z])([a-z0-9_]{2,})$/i",$data['filed']['name']))
		{	
		 $notice = &get_message(array('msg'=>'数据库字段名不符合规则','url'=>'','title'=>'Notice'));
		 $notice ->display();		 
	    }

		 $db = & get_db();		 
		 if($db->check_fields(DB_PREFIX."usertype_{$id}",array($data['filed']['name']=>'0')))
		 {
		 $notice = &get_message(array('msg'=>'此数据库字段名已存在,请更换一个！','url'=>'','title'=>'Notice'));
		 $notice ->display();			 
		 }
		 $filedstr = $this->get_field_str($data['filed']['name'],$data['filed']['type'],$data['filed']['length'],$data['filed']['isnull'],$data['filed']['defaultv'],'', $data['tips'],false);

		 $db->query("ALTER TABLE `".DB_PREFIX."usertype_{$id}` ADD ".$filedstr);	


		 $TB_module = &$this->_model('usertype'); 	 
         $field_one_info = $TB_module->oneshow($id);
		 
	     $field_config_data = unserialize($field_one_info['config']);
 
		 if(empty($field_config_data)||!is_array($field_config_data))
		 {
           $field_config_data = array();
		 }
		 	 
		 array_push($field_config_data,$data);
		 unset($dataconfig);		 
		 $dataconfig['config'] = serialize($field_config_data);		 
		 $TB_module->editaction($id,$dataconfig);
         $this->write_cache();
	     $this->produced_html($id);		 		 		 
	     $this->filedlist($id);


    }

   function filedpostedit()
    {

         $id = intval($_POST['id']);
         $tbid = intval($_POST['tbid']);
	     $data = $_POST['data'];
		 $tempdata = $temptype = '';
		 $temptype = $data['form']['type'];
		 $tempdata = $data['form'][$data['form']['type']];
		 unset($data['form']);
		 $data['form']['type'] = $temptype;
		 $data['form']['icon'] = $data['filed']['cname'];			 
		 $data['form'][$data['form']['type']] = $tempdata;
		 $data['form'][$data['form']['type']]['name'] = 'data['.$data['filed']['name'].']';		 
		 $data['form']['sx_data'] = $data['sx_data'];
		 unset($data['sx_data']);

	    if(!preg_match("/^([a-z])([a-z0-9_]{2,})$/i",$data['filed']['name']))
		{	
		 $notice = &get_message(array('msg'=>'数据库字段名不符合规则','url'=>'','title'=>'Notice'));
		 $notice ->display();		 
	    }

		 $db = & get_db();	
		 	 
/*		 if($db->check_fields(DB_PREFIX."content_{$id}",array($data['filed']['name']=>'0')))
		 {
		 $notice = &get_message(array('msg'=>'此数据库字段名已存在,请更换一个！','url'=>'','title'=>'Notice'));
		 $notice ->display();			 
		 }
*/



		 $TB_module = &$this->_model('usertype'); 	 
         $field_one_info = $TB_module->oneshow($id);
		 
	     $field_config_data = unserialize($field_one_info['config']);

		 if(empty($field_config_data)||!is_array($field_config_data))
		 {
           $field_config_data = array();
		 }

		 
		 $filedstr = $this->get_field_str($data['filed']['name'],$data['filed']['type'],$data['filed']['length'],$data['filed']['isnull'],$data['filed']['defaultv'],'', $data['tips'],false);		 	
		 $db->query("ALTER TABLE `".DB_PREFIX."usertype_{$id}` CHANGE `".$field_config_data[$tbid]['filed']['name']."` ".$filedstr);	 
		 		 
		 
         $field_config_data[$tbid] = $data;	
		 unset($dataconfig);		 
		 $dataconfig['config'] = serialize($field_config_data);		 
		 $TB_module->editaction($id,$dataconfig);
         $this->write_cache();	
	     $this->produced_html($id);		 	 		 
	     $this->filedlist($id);


    }

   function fileddel()
    {

         $id = intval($_GET['id']);
         $tbid = intval($_GET['tbid']);
		 $TB_module = &$this->_model('usertype'); 	 
         $field_one_info = $TB_module->oneshow($id);
		 
	     $field_config_data = unserialize($field_one_info['config']);
 
		 if(empty($field_config_data)||!is_array($field_config_data))
		 {
           $field_config_data = array();
		 }
		 $db = & get_db();		 
		 $db->query("ALTER TABLE `".DB_PREFIX."usertype_{$id}` DROP `".$field_config_data[$tbid]['filed']['name']."` ");	 
		 
         if(isset($field_config_data[$tbid]))
		 {
		 unset($field_config_data[$tbid]);
		 } 
		 
		 unset($dataconfig);		 
		 $dataconfig['config'] = serialize($field_config_data);		 
		 $TB_module->editaction($id,$dataconfig);
         $this->write_cache();
	     $this->produced_html($id);		 		 		 
	     $this->filedlist($id);

    }




   function viewlist()
    {

        $id = intval($_GET['id']);

        include(ROOT_PATH . '/lib/div.lib.php');
		$phpscupdiv= new PhpSCup_div($id,'','usertype/usertype_list.php');
        $phpscupdiv_array = $phpscupdiv->get_phpscupdiv($id,'usertype/usertype_list.php');
        $this->assign('phpscupdiv_array', $phpscupdiv_array);
	    $this->produced_html($id,'yes');

		 $TB_module = &$this->_model('usertype'); 
		 $data = $TB_module->oneshow($id);

	    if(is_file($this->view_scup_dir."/usertype_{$id}_edit.view.html"))
		{
		$view_file_index= $this->view_scup_dir."/usertype_{$id}_edit.view.html";		
	    }else
		{
		$view_file_index= $this->view_scup_dir.'/usertype/usertype_edit.view.html';
	    }
		
	    $view_scup_index=str_replace(array("<",">"),array("&lt;","&gt;"),read_file($view_file_index));


	    if(is_file($this->view_scup_dir."/usertype_reg_{$id}.view.html"))
		{
		$view_file_reg= $this->view_scup_dir."/usertype_reg_{$id}.view.html";		
	    }else
		{
		$view_file_reg= $this->view_scup_dir.'/usertype/usertype_reg.view.html';
	    }
		
	    $view_scup_reg=str_replace(array("<",">"),array("&lt;","&gt;"),read_file($view_file_reg));




	    if(is_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_list.view.html"))
		{
		$view_file_list= $GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_list.view.html";		
	    }else
		{
		$view_file_list=$GLOBALS['SCup']->SCup_view_root.'/usertype/usertype_list.view.html';
	    }
		
	    $view_admin_list=str_replace(array("<",">"),array("&lt;","&gt;"),read_file($view_file_list));

	    if(is_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_add.view.html"))
		{
		$view_file_add= $GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_add.view.html";		
	    }else
		{
		$view_file_add=$GLOBALS['SCup']->SCup_view_root.'/usertype/usertype_add.view.html';
	    }
		
	    $view_admin_add=str_replace(array("<",">"),array("&lt;","&gt;"),read_file($view_file_add));		
		

	    if(is_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_edit.view.html"))
		{
		$view_file_edit= $GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_edit.view.html";		
	    }else
		{
		$view_file_edit=$GLOBALS['SCup']->SCup_view_root."/usertype/usertype_edit.view.html";
	    }
		
	    $view_admin_edit=str_replace(array("<",">"),array("&lt;","&gt;"),read_file($view_file_edit));	

        $this->assign('view_file_index', $view_file_index);
        $this->assign('view_file_reg', $view_file_reg);		
		
        $this->assign('view_file_list', $view_file_list);
        $this->assign('view_file_add', $view_file_add);		
        $this->assign('view_file_edit', $view_file_edit);	

	    $this->assign('view_scup_index', $view_scup_index);
	    $this->assign('view_scup_reg', $view_scup_reg);		
		
        $this->assign('view_admin_list', $view_admin_list);
        $this->assign('view_admin_add', $view_admin_add);		
        $this->assign('view_admin_edit', $view_admin_edit);
		 
        $this->assign('id', $id);	   
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('usertype_viewlist.view.php');
    }

	function viewpost() 
	{
	    
        $id = intval($_POST['id']);
		$data = $_POST['data'];

		$data['scupindex']= str_replace(array('&quot;','&#39;'),array('"',"'"), $data['scupindex']);
		$data['scupreg']= str_replace(array('&quot;','&#39;'),array('"',"'"), $data['scupreg']);		
		
		$data['admin_list']= str_replace(array('&quot;','&#39;'),array('"',"'"), $data['admin_list']);
		$data['admin_add']= str_replace(array('&quot;','&#39;'),array('"',"'"), $data['admin_add']);		
		$data['admin_edit']= str_replace(array('&quot;','&#39;'),array('"',"'"), $data['admin_edit']);
				
	    $view_scup_index = stripslashes($data['scupindex']);
	    $view_scup_reg = stripslashes($data['scupreg']);		
		
	    $view_admin_list = stripslashes($data['admin_list']);
	    $view_admin_add = stripslashes($data['admin_add']);
	    $view_admin_edit = stripslashes($data['admin_edit']);

     	write_file($this->view_scup_dir."/usertype_{$id}_edit.view.html",$view_scup_index);
     	write_file($this->view_scup_dir."/usertype_reg_{$id}.view.html",$view_scup_reg);		
				
     	write_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_list.view.html",$view_admin_list);
	    write_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_add.view.html",$view_admin_add);
	    write_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_edit.view.html",$view_admin_edit);

   	    if(!is_writable($this->view_scup_dir."/usertype_{$id}_edit.view.html"))
		{	
		 $notice = &get_message(array('msg'=>$this->view_scup_dir."/usertype_{$id}_edit.view.html模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();		
	    }
			
   	    if(!is_writable($this->view_scup_dir."/usertype_reg_{$id}.view.html"))
		{	
		 $notice = &get_message(array('msg'=>$this->view_scup_dir."/usertype_reg_{$id}.view.html模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();		
	    }			
					

   	    if(!is_writable($GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_list.view.html"))
		{	
		 $notice = &get_message(array('msg'=>$GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_list.view.html模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();		
	    }
	    if(!is_writable($GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_add.view.html"))
		{	
		 $notice = &get_message(array('msg'=>$GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_add.view.html模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();		
	    }
	    if(!is_writable($GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_edit.view.html"))
	    {	
		 $notice = &get_message(array('msg'=>$GLOBALS['SCup']->SCup_view_root."/usertype_{$id}_edit.view.html模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();	
	    }
		 $notice = &get_message(array('msg'=>"模板修改成功",'url'=>'?app=usertype&act=viewlist&id='.$id,'title'=>'Notice'));
		 $notice ->display();					
		
	}
  	
	function get_module_data() 
	{
		return include (ROOT_PATH . '/data/module/module_div.php');
	}
  
	
    function write_cache()
    { 
		$TB_module = &$this->_model('usertype'); 		
		$infos = $TB_module->show();
		$infos_temp = array();		
		foreach($infos as $key => $value)
		{
		   $infos_temp[$value['id']] = $value;
		}	
		
		$cache_filename = ROOT_PATH . '/data/usertype/usertype_list.php';
		
	    write_file($cache_filename, "<?php \nreturn " . var_export($infos_temp , true) . ";\n?>");
		
		if(!is_writable($cache_filename))
		{
		exit('file directory is not writed power');//文件目录权限不可写
	    }
    }	

    function write_power_cache()
    { 
		$TB_module = &$this->_model('usertype'); 		
		$infos = $TB_module->show();
		$infos_temp = array();		
		array_var_sort($infos,'sort');		
		foreach($infos as $key => $value)
		{
		   $infos_temp[$value['appname']] = $value;
		}	
		
		$default_adminpower = include(ROOT_PATH .'/lib/power/admin.power.php');
		$default_scuppower = include(ROOT_PATH .'/lib/power/scup.power.php');


		foreach($infos_temp as $key=>$value)
		{
		      $default_adminpower[] = array 
       ('name' => $value['name'],
	    'app' => 'usertype',
		'act' => array 
                     ('index'=>'查看列表',
					  'add:addpost:kindeditor:kindeditorupfile:upfile:pupfile'=>'添加',	
					  'del'=>'删除',
					  'edit:editpost:kindeditor:kindeditorupfile:upfile:pupfile'=>'修改',					  																			
                     )
       );
		      $default_scuppower[] = array 
       ('name' => $value['alias'],
	    'app' => 'usertype',
		'act' => array 
                     ('index'=>'列表',
					  'show'=>'内容显示',	
					  'add:addpost:kindeditor:kindeditorupfile:upfile:pupfile'=>'添加',	
					  'del'=>'删除',
					  'edit:editpost:kindeditor:kindeditorupfile:upfile:pupfile'=>'修改',					  					  											
                     )
       );


		}


		$cache_filename_admin = ROOT_PATH .'/data/power/data/admin.power.php';
		$cache_filename_scup = ROOT_PATH .'/data/power/data/scup.power.php';	
		
	    write_file($cache_filename_admin, "<?php \nreturn " . var_export($default_adminpower , true) . ";\n?>");
	    write_file($cache_filename_scup, "<?php \nreturn " . var_export($default_scuppower , true) . ";\n?>");
		
		if(!is_writable($cache_filename_admin)||!is_writable($cache_filename_scup))
		{
		exit('file directory is not writed power');//文件目录权限不可写
	    }
    }	
		



    function write_module_default_scup_html($table_id=0)
    {	

	    $view_admin_edit = stripslashes(read_file($this->view_scup_dir.'/usertype/usertype_edit.view.html'));	
			
	    write_file($this->view_scup_dir."/usertype_{$table_id}_edit.view.html",$view_admin_edit);

	    if(!is_writable($this->view_scup_dir."/usertype_{$table_id}_edit.view.html"))
	    {	
		 $notice = &get_message(array('msg'=>$this->view_scup_dir."/usertype_{$table_id}_edit.view.html模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();	
	    }

    }


    function write_module_default_html($table_id=0)
    {	
		
	    $view_admin_list = stripslashes(read_file($GLOBALS['SCup']->SCup_view_root.'/usertype/usertype_list.view.html'));
	    $view_admin_add = stripslashes(read_file($GLOBALS['SCup']->SCup_view_root.'/usertype/usertype_add.view.html'));
	    $view_admin_edit = stripslashes(read_file($GLOBALS['SCup']->SCup_view_root.'/usertype/usertype_edit.view.html'));	
			
     	write_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_list.view.html",$view_admin_list);
	    write_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_add.view.html",$view_admin_add);
	    write_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_edit.view.html",$view_admin_edit);

   	    if(!is_writable($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_list.view.html"))
		{	
		 $notice = &get_message(array('msg'=>$GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_list.view.html模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();		
	    }
	    if(!is_writable($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_add.view.html"))
		{	
		 $notice = &get_message(array('msg'=>$GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_add.view.html模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();		
	    }
	    if(!is_writable($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_edit.view.html"))
	    {	
		 $notice = &get_message(array('msg'=>$GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_edit.view.html模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();	
	    }

    }

    function produced_html($table_id=0,$yes='no')
    {	

	    if(is_file($this->view_scup_dir."/usertype_{$table_id}_edit.view.html"))
		{
		$view_scup_index= $this->view_scup_dir."/usertype_{$table_id}_edit.view.html";		
	    }else
		{
		$view_scup_index=$this->view_scup_dir."/usertype/usertype_edit.view.html";
	    }

	    if(is_file($this->view_scup_dir."/usertype_reg_{$table_id}.view.html"))
		{
		$view_scup_reg= $this->view_scup_dir."/usertype_reg_{$table_id}.view.html";		
	    }else
		{
		$view_scup_reg=$this->view_scup_dir."/usertype/usertype_reg.view.html";
	    }


	    if(is_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_list.view.html"))
		{
		$view_file_list= $GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_list.view.html";		
	    }else
		{
		$view_file_list=$GLOBALS['SCup']->SCup_view_root.'/usertype/usertype_list.view.html';
	    }
		
	    if(is_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_add.view.html"))
		{
		$view_file_add= $GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_add.view.html";		
	    }else
		{
		$view_file_add=$GLOBALS['SCup']->SCup_view_root.'/usertype/usertype_add.view.html';
	    }
		
	    if(is_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_edit.view.html"))
		{
		$view_file_edit= $GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_edit.view.html";		
	    }else
		{
		$view_file_edit=$GLOBALS['SCup']->SCup_view_root."/usertype/usertype_edit.view.html";
	    }
		
	    $view_scup_edit=read_file($view_scup_index);
		
	    $view_scup_reg=read_file($view_scup_reg);				
		
	    $view_admin_list=read_file($view_file_list);
	    $view_admin_add=read_file($view_file_add);			
	    $view_admin_edit=read_file($view_file_edit);
			
        include_once(ROOT_PATH . '/lib/div.lib.php');
		$phpscupdiv= new PhpSCup_div($table_id,'usercon','usertype/usertype_list.php');
        $phpscupdiv_array = $phpscupdiv->get_phpscupdiv($table_id,'usertype/usertype_list.php');
		$phpscupdiv_temp_array = array();
        foreach($phpscupdiv_array as $key =>$value)
		{
		 $phpscupdiv_temp_array[$value['icon']] = str_replace(array("&lt;","&gt;"),array("<",">"),$value['phpdiv']);
		} 
	
        $phpscuplistarray = $phpscupdiv->get_module_list_array($table_id,'usertype/usertype_list.php');				
        $view_admin_list = $phpscupdiv->_get_content_html($view_admin_list,$phpscuplistarray);
        $view_admin_add = $phpscupdiv->_get_content_html($view_admin_add,$phpscupdiv_temp_array);		
        $view_admin_edit = $phpscupdiv->_get_content_html($view_admin_edit,$phpscupdiv_temp_array);	

        $view_scup_edit = $phpscupdiv->_get_content_html($view_scup_edit,$phpscupdiv_temp_array);
		
        $view_scup_reg = $phpscupdiv->_get_content_html($view_scup_reg,$phpscupdiv_temp_array);
		
     	write_file($this->view_scup_dir."/usertype_{$table_id}_edit.view.html",$view_scup_edit);					
			
     	write_file($this->view_scup_dir."/usertype_reg_{$table_id}.view.html",$view_scup_reg);
					
     	write_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_list.view.html",$view_admin_list);
	    write_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_add.view.html",$view_admin_add);
	    write_file($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_edit.view.html",$view_admin_edit);

   	    if(!is_writable($this->view_scup_dir."/usertype_reg_{$table_id}.view.html"))
		{	
		 $notice = &get_message(array('msg'=>$this->view_scup_dir."/usertype_reg_{$table_id}.view.html模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();		
	    }
		
   	    if(!is_writable($this->view_scup_dir."/usertype_{$table_id}_edit.view.html"))
		{	
		 $notice = &get_message(array('msg'=>$this->view_scup_dir."/usertype_{$table_id}_edit.view.html模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();		
	    }		
				
   	    if(!is_writable($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_list.view.html"))
		{	
		 $notice = &get_message(array('msg'=>$GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_list.view.html模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();		
	    }
	    if(!is_writable($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_add.view.html"))
		{	
		 $notice = &get_message(array('msg'=>$GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_add.view.html模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();		
	    }
	    if(!is_writable($GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_edit.view.html"))
	    {	
		 $notice = &get_message(array('msg'=>$GLOBALS['SCup']->SCup_view_root."/usertype_{$table_id}_edit.view.html模板生成失败,有可能是目录权限不可写,请手工创建一个,复制代码进去",'url'=>'','title'=>'Notice'));
		 $notice ->display();	
	    }
		 if($yes=='no')
		 {
		 $notice = &get_message(array('msg'=>"模块修改成功!",'url'=>'?app=cache','title'=>'Notice'));
		 $notice ->display();
		 }
    }
	
    function get_field_str($name,$type,$length = '',$isnull = false,$default = '',$extra = '', $comment = '',$default_orig = false)
    {

        $is_timestamp = strpos(' ' . strtoupper($type), 'TIMESTAMP') == 1;

        $query = $this->str_backquote($name) . ' ' . $type;

        if ($length != '' && !preg_match('@^(data|datetime|time|tinyblob|tinytext|blob|text|mediumblob|mediumtext|longblob|longtext)$@i', $type)) 
		{
            $query .= '(' . $length . ')';
        }
        if ($isnull !== false) 
		{
            if (!empty($isnull)) 
			{
                $query .= ' NOT NULL';
            } else 
			{
                $query .= ' NULL';
            }
        }
		if ($extra !== 'AUTO_INCREMENT' && (strlen($default) || $default != $default_orig)) 
		{
            if (strtoupper($default) == 'NULL') 
			{
                $query .= ' DEFAULT NULL';
            } else 
			{
                if (strlen($default)) 
				{
                    if ($is_timestamp) 
					{
                        $query .= ' DEFAULT ' . $this->str_sqlAddslashes($default);
                    } else 
					{
                        $query .= ' DEFAULT \'' . $this->str_sqlAddslashes($default) . '\'';
                    }
                }
            }
        }

        if (!empty($extra)) 
		{
            $query .= ' ' . $extra;

        }
		
        if (get_db()->version() >= '4.1' && !empty($comment)) 
		{
            $query .= " COMMENT '" . $this->str_sqlAddslashes($comment) . "'";
        }
        return $query;
    } 

    function str_backquote($name)
    {
        if (strlen($name) && $name !== '*') 
		{
            return '`' . str_replace('`', '``', $name) . '`';
        } else 
		{
            return $name;
        }
    } 

    function str_sqlAddslashes($string = '')
    {
        $string = str_replace('\\', '\\\\', $string);
        $string = str_replace('\'', '\'\'', $string);
        return $string;
    }
		


    function get_sx_cache($id)
    {

        $data = include(ROOT_PATH.'/data/sx/sx_'.$id.'.php');  
		return $data['name'];
    }
	
    function get_role_data($str='')
    {

		$TB_role = &$this->_model('role');			
		$roledata_temp = $TB_role->selectshow();
        $roleicon = role_con(ROLE_CON_FILE);
		$roledata = array();
		foreach($roledata_temp as $key => $value)
		{
		    $str = '';
			if(isset($value['role_name']))
			{
			     foreach($roleicon as $key1 => $value1)
				 {
				     $str .= $value1.':'.$value['role_id'].'|';
				 }			     
				 $roledata[$value['role_id']]['name'] = $value['role_name'];
				 $roledata[$value['role_id']]['value'] = substr($str, 0, -1);
			}
		}	   
	    return $roledata;
    }
}



    function role_con($str)
    {
	      $array = array();
          $str_array = explode('|',$str);
	      foreach($str_array as $key => $value)
	      {
	            if($value)
		        {
			   
			   $array[$key] = $value;
		   
		        }
	 
	      }    
	   
	      return $array; 		

    }
	
	

?>